
void createSkybox(StaticObject *skybox)
{
     Location pos;
     pos.x = 0.0;
     pos.y = 0.0;
     pos.z = -1.0; //-24
     skybox->pos = pos;
     
     skybox->mesh = (ObjectMesh*)malloc(sizeof(ObjectMesh));
     
	 XYZ rotation;
	 rotation.x = 0.0;
	 rotation.y = 0.0;
	 rotation.z = 0.0;
	 
	 initObjectMesh(skybox->mesh, OS_OBJ_PATH(cylinder_test.obj), 19, rotation);//renderObjects
	 addMeshTexture(skybox->mesh, OS_IMG_PATH(cylinder_test.tga) );//renderObjects
	
    
    
}

//void drawSkybox(StaticObject *skybox)
//{
//    glPushMatrix();
//    glTranslatef(skybox->pos.x,skybox->pos.y,skybox->pos.z);
//    glScalef(-1.0, -1.0 ,-1.0);
//    drawMeshVBO(skybox->mesh);
//    
//    glPopMatrix();
//    
//}


/**
 *	setupTextOverlay() defines the standard viewport to output text. 
 *  
 */
 void setupTextOverlay( ) {
    
    int width, height;
    glfwGetWindowSize( &width, &height );
    if(height<=0) height=1; // Safeguard against iconified/closed window
    glViewport( 0, 0, width, height ); // The entire window
    glMatrixMode(GL_PROJECTION); // "We want to edit the projection matrix"
    glLoadIdentity(); // Reset the matrix to identity
    gluPerspective( 50.0f, (GLfloat)width/(GLfloat)height, 1.0f, 200.0f );
    glMatrixMode( GL_MODELVIEW ); // "We want to edit the modelview matrix"
    glMatrixMode( GL_MODELVIEW ); // "We want to edit the modelview matrix"
    glLoadIdentity(); // Reset the matrix to identity
}


/*=========================================================================================================
    setupCameraPlayer1 was originally supposed to handle player2's splitscreen 
    but it worked the other way around.... strange
 ==========================================================================================================*/
/**
 *	setupCameraPlayer1() defines the viewport for player 1. 
 *  
 */
void setupCameraPlayer1( ) {
    
    int width, height;
    glfwGetWindowSize( &width, &height );
    if(height<=0) height=1; // Safeguard against iconified/closed window
    glViewport( 0, height/2, width, height/2 ); // The entire window
    glMatrixMode(GL_PROJECTION); // "We want to edit the projection matrix"
    glLoadIdentity(); // Reset the matrix to identity
    gluPerspective( 50.0f, (GLfloat)width/(GLfloat)height*2, 1.0f, 200.0f );
    glMatrixMode( GL_MODELVIEW ); // "We want to edit the modelview matrix"
    glMatrixMode( GL_MODELVIEW ); // "We want to edit the modelview matrix"
    glLoadIdentity(); // Reset the matrix to identity
}

/**
 *	setupCameraPlayer2() defines the viewport for player 2. 
 *  
 */
void setupCameraPlayer2( ) {
    
    int width, height;
    glfwGetWindowSize( &width, &height );
    if(height<=0) height=1; // Safeguard against iconified/closed window
    glViewport( 0, 0, width, height/2 ); // The entire window
    glMatrixMode(GL_PROJECTION); // "We want to edit the projection matrix"
    glLoadIdentity(); // Reset the matrix to identity
    gluPerspective( 50.0f, (GLfloat)width/(GLfloat)height*2, 1.0f, 200.0f );
    glMatrixMode( GL_MODELVIEW ); // "We want to edit the modelview matrix"
    glMatrixMode( GL_MODELVIEW ); // "We want to edit the modelview matrix"
    glLoadIdentity(); // Reset the matrix to identity
}


/**
 *	setupCameraPlayer2() defines the viewport for player 2. 
 *  
 */
void setupMenuCamera( ) {
    
    int width, height;
    glfwGetWindowSize( &width, &height );
    if(height<=0) height=1; // Safeguard against iconified/closed window
    glViewport( 0, 0, width, height); // The entire window
    glMatrixMode(GL_PROJECTION); // "We want to edit the projection matrix"
    glLoadIdentity(); // Reset the matrix to identity
    gluPerspective( 40.0f, (GLfloat)width/(GLfloat)height, 1.0f, 400.0f );
    glMatrixMode( GL_MODELVIEW ); // "We want to edit the modelview matrix"
    glMatrixMode( GL_MODELVIEW ); // "We want to edit the modelview matrix"
    glLoadIdentity(); // Reset the matrix to identity
}


/**
 *	followCamera() translates the the world to follow player. 
 *  @param PlayerObject *player = a pointer to the player
 */
void followCamera(PlayerObject *player, StaticObject *skybox) {
    Location camera;
    camera.x = -5.0f; camera.y = 0.0f; camera.z = 3.0f;
    gluLookAt( camera.x, camera.y, camera.z,    // Eye position
			  0, 0, 2.0f,                       // View point
			  0.0f, 0.0f, 1.0f );               // Up vector
	
	 
    
	glRotatef(-(*player).cameraAngle * 360 / M_PI / 2, 0, 0, 1.0f);	
	
     	  
	glRotatef(-(*player).angle.z * 360 / M_PI / 2, 0, 0, 1.0f);
    glTranslatef(-(*player).pos.x, -(*player).pos.y, 0.0f);
    
    /*========================================================================
	 Skybox
	 =========================================================================*/
	 glPushMatrix();
	 
     glTranslatef(skybox->pos.x,skybox->pos.y,skybox->pos.z);
     glTranslatef((*player).pos.x, (*player).pos.y, 0.0f);
     //glScalef(-1.0, -1.0 ,-1.0);
     drawMeshVBO(skybox->mesh);
     glPopMatrix();
   

	
	// Set up light
//	float vector4f_center[4]={0.0f, 0.0f, 100.0f, 2.0f}; // Big Sun in origo	
//	GLfloat ambient[] = { 0.2f, 0.2f, 0.2f };
//	
//	
//	glLightfv(GL_LIGHT0, GL_POSITION, vector4f_center); // Set light position			
//	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
//	glEnable(GL_LIGHTING);
//	glEnable(GL_LIGHT0);	
	
}

void menuCamera(StaticObject *skybox) {
	float time = glfwGetTime();

    Location camera;
	Location focusPoint;

    camera.x = 80*cos(time/16);//80.0f; 
	camera.y = 80*sin(time/16);//80.0f; 
	camera.z = 40+10*cos(time/8);//30.0f;
	
	focusPoint.x = -40*cos(time/8);
	focusPoint.y = -40*sin(time/8);
	focusPoint.z = 0;

    gluLookAt( camera.x, camera.y, camera.z,    // Eye position
			  focusPoint.x,focusPoint.y,focusPoint.z,                       // View point
			  0.0f, 0.0f, 1.0f );               // Up vector

    /*========================================================================
	 Skybox
	 =========================================================================*/
	glPushMatrix();
	glTranslatef(skybox->pos.x,skybox->pos.y,skybox->pos.z);
	drawMeshVBO(skybox->mesh);
	glPopMatrix();
	
	
}









